﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class CoilTrackConnForm : System.Windows.Forms.Form
	{
		public CoilTrackConnForm(Autodesk.Revit.DB.Document document)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = document;
			this.userSetting = new HVACUserSetting();
			base.Icon = Resource.YArchitechIcon;
		}

		private void CoilTrackConnForm_Load(object sender, EventArgs e)
		{
			this.InitialInterface();
		}

		private void InitialInterface()
		{
			CoilTrackConn coilTrackConn = this.userSetting.GetCoilTrackConn();
			this.BindSystemType(this.doc, this.cmbSupplySystem, coilTrackConn.SupplySysName, 1);
			this.BindPipeType(this.doc, this.cmbSupplyPipeType, coilTrackConn.SupplyPipeName);
			this.BindSystemType(this.doc, this.cmbReturnSystem, coilTrackConn.ReturnSysName, 2);
			this.BindPipeType(this.doc, this.cmbReturnPipeType, coilTrackConn.ReturnPipeName);
			this.BindSystemType(this.doc, this.cmbSanitarySystem, coilTrackConn.SanitarySysName, 3);
			this.BindPipeType(this.doc, this.cmbSanitaryPipeType, coilTrackConn.SanitaryPipeName);
			this.txtSupplyDia.Text = coilTrackConn.SupplyDiameter.ToString();
			this.txtSupplyDistance.Text = coilTrackConn.SupplyDistance.ToString();
			this.txtSupplyOffset.Text = coilTrackConn.SupplyOffset.ToString();
			this.txtReturnDia.Text = coilTrackConn.ReturnDiameter.ToString();
			this.txtReturnDistance.Text = coilTrackConn.ReturnDistance.ToString();
			this.txtReturnOffset.Text = coilTrackConn.ReturnOffset.ToString();
			this.txtSanitaryDia.Text = coilTrackConn.SanitaryDiameter.ToString();
			this.txtSanitaryDistance.Text = coilTrackConn.SanitaryDistance.ToString();
			this.txtSanitaryOffset.Text = coilTrackConn.SanitaryOffset.ToString();
		}

		private void TestInfo(CoilTrackConn cConn)
		{
			string.Concat(new object[]
			{
				cConn.SupplyPipeName,
				"\n",
				cConn.SupplySysName,
				"\n",
				cConn.SupplyDiameter,
				" ",
				cConn.SupplyDistance,
				" ",
				cConn.SupplyOffset,
				"*\n"
			});
			string.Concat(new object[]
			{
				cConn.ReturnPipeName,
				"\n",
				cConn.ReturnSysName,
				"\n",
				cConn.ReturnDiameter,
				" ",
				cConn.ReturnDistance,
				" ",
				cConn.ReturnOffset,
				"-\n"
			});
			YJKMessageBox.Show(string.Concat(new object[]
			{
				cConn.SanitaryPipeName,
				"\n",
				cConn.SanitarySysName,
				"\n",
				cConn.SanitaryDiameter,
				" ",
				cConn.SanitaryDistance,
				" ",
				cConn.SanitaryOffset,
				" +"
			}));
		}

		private void BindSystemType(Autodesk.Revit.DB.Document doc, System.Windows.Forms.ComboBox cmBox, string stName, int sysTypeNum)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			new List<Element>();
			List<Element> list = filteredElementCollector.OfClass(typeof(PipingSystemType)).ToList<Element>();
			List<PipingSystemType> list2 = new List<PipingSystemType>();
			foreach (Element element in list)
			{
				PipingSystemType pipingSystemType = element as PipingSystemType;
				if (sysTypeNum == 1 && (int)pipingSystemType.SystemClassification == 7)
				{
					list2.Add(pipingSystemType);
				}
				if (sysTypeNum == 2 && (int)pipingSystemType.SystemClassification == 8)
				{
					list2.Add(pipingSystemType);
				}
				if (sysTypeNum == 3 && ((int)pipingSystemType.SystemClassification == 16 || (int)pipingSystemType.SystemClassification == 22))
				{
					list2.Add(pipingSystemType);
				}
			}
			List<HYPipeSysType> list3 = new List<HYPipeSysType>();
			int num = 0;
			int selectedIndex = 0;
			foreach (PipingSystemType pipingSystemType2 in list2)
			{
				list3.Add(new HYPipeSysType
				{
					SysTypeId = pipingSystemType2.Id,
					SysTypeName = pipingSystemType2.Name
				});
				if (pipingSystemType2.Name == stName)
				{
					selectedIndex = num;
				}
				num++;
			}
			cmBox.DataSource = list3;
			cmBox.ValueMember = "SysTypeId";
			cmBox.DisplayMember = "SysTypeName";
			cmBox.SelectedIndex = selectedIndex;
		}

		private void BindPipeType(Autodesk.Revit.DB.Document doc, System.Windows.Forms.ComboBox cmBox, string ptName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			new List<Element>();
			List<Element> list = filteredElementCollector.OfClass(typeof(PipeType)).ToList<Element>();
			List<HYPipeType> list2 = new List<HYPipeType>();
			int num = 0;
			int selectedIndex = 0;
			foreach (Element element in list)
			{
				list2.Add(new HYPipeType
				{
					PipeTypeId = element.Id,
					PipeTypeName = element.Name
				});
				if (element.Name == ptName)
				{
					selectedIndex = num;
				}
				num++;
			}
			cmBox.DataSource = list2;
			cmBox.ValueMember = "PipeTypeId";
			cmBox.DisplayMember = "PipeTypeName";
			cmBox.SelectedIndex = selectedIndex;
		}

		private void txtSupplyDia_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			try
			{
				int num = Convert.ToInt32(hytextBox.Text);
				if (num < 10 || num > 5000)
				{
					YJKMessageBox.Information("管道直径应在大于10且小于5000。");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("请输入正整数。");
				e.Cancel = true;
			}
		}

		private void txtSupplyOffset_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			try
			{
				double num = Convert.ToDouble(hytextBox.Text);
				if (num < -20.0 || num > 20.0)
				{
					YJKMessageBox.Information("管道相对标高应在±20m范围内。");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("请输入数字。");
				e.Cancel = true;
			}
		}

		private void txtSupplyDistance_Validating(object sender, CancelEventArgs e)
		{
			YJKTextBox hytextBox = sender as YJKTextBox;
			if (hytextBox == null)
			{
				return;
			}
			try
			{
				int num = Convert.ToInt32(hytextBox.Text);
				if (num < -5000 || num > 5000)
				{
					YJKMessageBox.Information("管道直径应在±5000mm范围内。");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("请输入整数。");
				e.Cancel = true;
			}
		}

		private void btnConn_Click(object sender, EventArgs e)
		{
			this.CoilConn = new CoilTrackConn();
			this.CoilConn.SupplyDiameter = Convert.ToInt32(this.txtSupplyDia.Text);
			this.CoilConn.SupplyDistance = Convert.ToInt32(this.txtSupplyDistance.Text);
			this.CoilConn.SupplyOffset = Convert.ToDouble(this.txtSupplyOffset.Text);
			this.CoilConn.SupplySysName = this.cmbSupplySystem.Text;
			this.CoilConn.SupplyPipeName = this.cmbSupplyPipeType.Text;
			this.CoilConn.ReturnDiameter = Convert.ToInt32(this.txtReturnDia.Text);
			this.CoilConn.ReturnDistance = Convert.ToInt32(this.txtReturnDistance.Text);
			this.CoilConn.ReturnOffset = Convert.ToDouble(this.txtReturnOffset.Text);
			this.CoilConn.ReturnSysName = this.cmbReturnSystem.Text;
			this.CoilConn.ReturnPipeName = this.cmbReturnPipeType.Text;
			this.CoilConn.SanitaryDiameter = Convert.ToInt32(this.txtSanitaryDia.Text);
			this.CoilConn.SanitaryDistance = Convert.ToInt32(this.txtSanitaryDistance.Text);
			this.CoilConn.SanitaryOffset = Convert.ToDouble(this.txtSanitaryOffset.Text);
			this.CoilConn.SanitarySysName = this.cmbSanitarySystem.Text;
			this.CoilConn.SanitaryPipeName = this.cmbSanitaryPipeType.Text;
			this.userSetting.WriteCoilTrackConn(this.CoilConn);
			this.ConnSetId = new HYConnSetId();
			this.ConnSetId.SupplySysId = (ElementId)this.cmbSupplySystem.SelectedValue;
			this.ConnSetId.SupplyPipeTypeId = (ElementId)this.cmbSupplyPipeType.SelectedValue;
			this.ConnSetId.ReturnSysId = (ElementId)this.cmbReturnSystem.SelectedValue;
			this.ConnSetId.ReturnPipeTypeId = (ElementId)this.cmbReturnPipeType.SelectedValue;
			this.ConnSetId.SanitarySysId = (ElementId)this.cmbSanitarySystem.SelectedValue;
			this.ConnSetId.SanitaryPipeTypeId = (ElementId)this.cmbSanitaryPipeType.SelectedValue;
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		public CoilTrackConn CoilConn
		{
			get
			{
				return this.coilConn;
			}
			set
			{
				this.coilConn = value;
			}
		}

		public HYConnSetId ConnSetId
		{
			get
			{
				return this.connSetId;
			}
			set
			{
				this.connSetId = value;
			}
		}

		private Document doc;

		private CoilTrackConn coilConn;

		private HVACUserSetting userSetting;

		private HYConnSetId connSetId;
	}
}
